package com.py.chat.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.py.chat.bean.Exp;

public class DrawExpUtil {

	// 先获取所有概率对象 将概率集合转化为数轴
	public static int getExp(){
		// 先获取所有概率对象
		String json = PropertiesUtil.getValue("exp");
		Gson gson = new Gson();
		List<Exp> exps = gson.fromJson(json, new TypeToken<List<Exp>>(){}.getType());
		if(exps == null){
			return 0;
		}
		// 将概率集合转化为数轴
		Double sumRate = 0D;
		for(Exp exp:exps){
			sumRate += exp.getProbability();
		}
		if(sumRate == 0){
			return 0;
		}
		Double rate = 0D;
		List<Double> rateList = new ArrayList<>();
		for(Exp exp:exps){
			rate += exp.getProbability();
			rateList.add(rate/sumRate);
		}
		// 将随机值放到list中 再排序
		Double random = Math.random();
		rateList.add(random);
		Collections.sort(rateList);
		rateList.indexOf(random);
		return exps.get(rateList.indexOf(random)).getExp();
	}
	public static void main(String[] args) {
		for(int i=0;i<50;i++){
			System.err.println(getExp());
		}
	}
}
